package org.osll.tictactoe.engine;

import java.io.Serializable;
import java.util.Vector;

import org.osll.tictactoe.Field;
import org.osll.tictactoe.Move;
import org.osll.tictactoe.Size;
import org.osll.tictactoe.Team;

public class FieldImpl implements Field, Size, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5585877074803814043L;
	private Vector<Team> field = new Vector<Team>();
	private int dimX;
	private int dimY;
	
	public FieldImpl(int rows, int cols) {
		dimX = new Integer(rows);
		dimY = new Integer(cols);
		field = new Vector<Team>();
		for(int i = 0; i< rows; ++i) {
			for(int j = 0; j<cols; ++j)
			   field.add(Team.U);
		}
	}
	
	
	
	public FieldImpl() {
		System.out.println("create deafult field");
	}
	
	@Override
	public Team getPlayer(int row, int col) {
		return field.get(row*dimX+col);
	}

	@Override
	public Size getSize() {
		return this;
	}
	
	@Override
	public int getHeight() {
		return dimY;
	}

	@Override
	public int getWidth() {
		return dimX;
	}
	
	public Team getTeam(int row, int col) {
		Team t = null;
		try {
			t = field.get(row*dimX+col);
		} catch (ArrayIndexOutOfBoundsException e) {
		}
		return t;
	}
	
	public void addMove(Move move) {
		if(getTeam(move.getRow(),move.getCol())!=Team.U)
			throw new IllegalStateException("Данный ход недопустим");
		field.set(move.getRow()*dimX+move.getCol(),move.getTeam());
	}

//	public String toString() {
//		String s = "";
//		for(int i=0;i<getHeight();++i) {
//			for(int j=0;j<getWidth();++j)
//				s += getTeam(i, j) + " ";
//			s += "\n";
//		}
//		return s;
//	}
	
	
}
